import {createAsyncThunk, createSlice} from "@reduxjs/toolkit";
import {
    getHomeDiscountData,
    getHomeGoodPriceData,
    getHomeHighScoreData,
    getHomeHotRecommendData
} from "@/services/modules/home";

export const fetchHomeGoodPriceDataAction = createAsyncThunk(
    "good-price",
    (plyload, {dispatch}) => {
        getHomeGoodPriceData().then((res) => {
            dispatch(changeGoodPriceInfo(res));
        });
        getHomeHighScoreData().then((res) => {
            dispatch(changeHighScoreInfoAction(res));
        });
        getHomeDiscountData().then((res) => {
            console.log(res);
            dispatch(changeDiscountDataAction(res));
        });

        getHomeHotRecommendData().then((res) => {
            console.log(res);
            dispatch(getHomeHotRecommendDataAction(res));
        });


    }
);

const homeSlice = createSlice({
    name: "home",
    initialState: {
        goodPriceInfo: [],
        highScoreInfo: [],
        discountInfo: [],
        hotRecommendInfo: [],
    },
    reducers: {
        changeGoodPriceInfo(state, {payload}) {
            state.goodPriceInfo = payload;
        },
        changeHighScoreInfoAction(state, {payload}) {
            state.highScoreInfo = payload;
        },
        changeDiscountDataAction(state, {payload}) {
            state.discountInfo = payload;
        },
        getHomeHotRecommendDataAction(state, {payload}) {
            state.hotRecommendInfo = payload;
        },
    },
});

export const {
    changeGoodPriceInfo,
    changeHighScoreInfoAction,
    changeDiscountDataAction,
    getHomeHotRecommendDataAction,
} = homeSlice.actions;
export default homeSlice.reducer;
